using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SynoManager.FileStation.Tasks;

namespace SynoManager.FileStation
{
    public class FileStationService : ServiceBase
    {
        internal FileStationClient client;
        private FileStationGuiPlugin plugin;
        private TaskQueue taskQueue;

        public FileStationGuiPlugin Plugin
        {
            get { return plugin; }
            set { plugin = value; }
        }

        public TaskQueue TaskQueue
        {
            get
            {
                if (taskQueue == null)
                    taskQueue = new TaskQueue(this);

                return taskQueue;
            }
        }

        public override void Initialize(SynoDevice device)
        {
            base.Initialize(device);
            client = new FileStationClient(Device);
        }

        public SharedFolderInfo[] GetSharedFolders()
        {
            var folders = client.GetShares();
            List<SharedFolderInfo> result = new List<SharedFolderInfo>();
            foreach(var folder in folders)
            {
                result.Add(new SharedFolderInfo(this, folder));
            }
            return result.ToArray();
        }

        internal DirectoryInfo GetDirectory(string path)
        {
            var files = client.GetFiles(path, "all", 0, 999, "filename", "ASC");
            return new DirectoryInfo(this, path, files);
        }

        internal DeviceRoot GetRoot()
        {
            return new DeviceRoot(this);
        }

        internal VolumeInfo GetVolumeInfo(string path)
        {
            if (path == "/")
            {
                return null;
            }
            else
            {
                return new VolumeInfo(client.GetFS(path));
            }
        }
    }


}
